﻿
Imports std
Imports counter

'a helper function set, directly call from name
Namespace counter2
    Public Module _counter2
        Private m As map(Of String, Int64) = Nothing

        Public Function increase(ByVal name As String, Optional ByVal c As Int64 = 1, _
                                 Optional ByVal writeCount As Boolean = True, _
                                 Optional ByVal writeAverage As Boolean = False, _
                                 Optional ByVal writeLastAverage As Boolean = False, _
                                 Optional ByVal lastAverageLength As Int64 = defaultLastAverageLength, _
                                 Optional ByVal autoInitialize As Boolean = True) As Boolean
            On Error Resume Next
            Dim id As Int64
            SyncLock m
                Dim it As map(Of String, Int64).iterator = Nothing
                it = m.find(name)
                If it = m.end() Then
                    id = register(name,
                                  writeCount, writeAverage, writeLastAverage, lastAverageLength, _
                                  autoInitialize)
                    m(name) = id
                Else
                    id = (+it).second
                End If
            End SyncLock
            Return counter.increase(id, c)
        End Function

        Public Function decrease(ByVal name As String, Optional ByVal c As Int64 = 1, _
                                 Optional ByVal writeCount As Boolean = True, _
                                 Optional ByVal writeAverage As Boolean = False, _
                                 Optional ByVal writeLastAverage As Boolean = False, _
                                 Optional ByVal lastAverageLength As Int64 = defaultLastAverageLength, _
                                 Optional ByVal autoInitialize As Boolean = True) As Boolean
            On Error Resume Next
            Return increase(name, -c,
                            writeCount, writeAverage, writeLastAverage, lastAverageLength, autoInitialize)
        End Function

        Public Function increaseCounter(ByVal name As String, Optional ByVal c As Int64 = 1, _
                                        Optional ByVal autoInitialize As Boolean = True) As Boolean
            On Error Resume Next
            Return increase(name, c, True, False, False, autoInitialize:=autoInitialize)
        End Function

        Public Function decreaseCounter(ByVal name As String, Optional ByVal c As Int64 = 1, _
                                        Optional ByVal autoInitialize As Boolean = True) As Boolean
            On Error Resume Next
            Return decrease(name, c, True, False, False, autoInitialize:=autoInitialize)
        End Function

        Public Function increaseAverage(ByVal name As String, Optional ByVal c As Int64 = 1, _
                                        Optional ByVal autoInitialize As Boolean = True) As Boolean
            On Error Resume Next
            Return increase(name, c, False, True, False, autoInitialize:=autoInitialize)
        End Function

        Public Function decreaseAverage(ByVal name As String, Optional ByVal c As Int64 = 1, _
                                        Optional ByVal autoInitialize As Boolean = True) As Boolean
            On Error Resume Next
            Return decrease(name, c, False, True, False, autoInitialize:=autoInitialize)
        End Function

        Public Function increaseLastAverage(ByVal name As String, Optional ByVal c As Int64 = 1, _
                                            Optional ByVal lastAverageLength As Int64 = defaultLastAverageLength, _
                                            Optional ByVal autoInitialize As Boolean = True) As Boolean
            On Error Resume Next
            Return increase(name, c, False, False, True, lastAverageLength, autoInitialize)
        End Function

        Public Function decreaseLastAverage(ByVal name As String, Optional ByVal c As Int64 = 1, _
                                            Optional ByVal lastAverageLength As Int64 = defaultLastAverageLength, _
                                            Optional ByVal autoInitialize As Boolean = True) As Boolean
            On Error Resume Next
            Return decrease(name, c, False, False, True, lastAverageLength, autoInitialize)
        End Function

        Public Function increaseAverageAndLastAverage(ByVal name As String, Optional ByVal c As Int64 = 1, _
                                                      Optional ByVal lastAverageLength As Int64 = _
                                                          defaultLastAverageLength, _
                                                      Optional ByVal autoInitialize As Boolean = True) As Boolean
            On Error Resume Next
            Return increase(name, c, False, True, True, lastAverageLength, autoInitialize)
        End Function

        Public Function decreaseAverageAndLastAverage(ByVal name As String, Optional ByVal c As Int64 = 1, _
                                                      Optional ByVal lastAverageLength As Int64 = _
                                                          defaultLastAverageLength, _
                                                      Optional ByVal autoInitialize As Boolean = True) As Boolean
            On Error Resume Next
            Return decrease(name, c, False, True, True, lastAverageLength, autoInitialize)
        End Function

        Sub New()
            m = New map(Of String, Int64)()
        End Sub
    End Module
End Namespace
